<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="20" class="myLineHeight_40px">
        <span v-if="type=='add'">新增楼层</span>
        <span v-if="type=='update'">编辑楼层</span>
        <span v-if="type=='info'">楼层详情</span>
      </el-col>
    </el-row>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 所属片区 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="districtSelected">
            <el-select v-model="ruleForm.districtSelected"
              @visible-change="clickDistrictSelect"
              @change="changeDistrictSelect"
             :disabled="is_disabled"
              :loading="loading"
              placeholder="请选择片区">
              <el-option
                v-for="item in districtList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 所属小区 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="areaSelected">
            <el-select v-model="ruleForm.areaSelected"
             @visible-change="clickAreaSelect"
             @change="changeAreaSelect"
             :disabled="is_disabled"
              :loading="loading"
              placeholder="请选择小区">
              <el-option
                v-for="item in areaList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 所属楼栋 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="buildingSelected">
            <el-select v-model="ruleForm.buildingSelected"
              @visible-change="clickBuildingSelect"
              @change="changeBuildingSelect"
              :disabled="is_disabled"
              :loading="loading"
              class="w94" placeholder="请选择楼栋">
              <el-option
                v-for="item in buildingList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 所属单元 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="cellSelected">
            <el-select v-model="ruleForm.cellSelected"
              @visible-change="clickCellSelect"
              :loading="loading"
              :disabled="is_disabled"
              class="w94" placeholder="请选择单元">
              <el-option
                v-for="item in cellList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          <span class="require myVA_M">*</span> 楼层名称 :
        </el-col>
        <el-col :span="7">
          <el-form-item prop="floor_name">
            <el-input  v-model="ruleForm.floor_name" :readonly="is_modifly" placeholder="请输入楼层名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          楼层编号 :
        </el-col>
        <el-col :span="7">
          <el-input v-model="ruleForm.floor_no" placeholder="请输入楼层编号" :readonly="is_modifly"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="3" class="myTextAlignRight myLineHeight_40px">
          备注 :
        </el-col>
        <el-col :span="7">
          <el-input type="textarea" :readonly="is_modifly" v-model="ruleForm.extra_desc" placeholder="备注"></el-input>
        </el-col>
      </el-row>
      <el-row class="myMarginTop_10px" :gutter="10">
        <el-col :span="10" :offset="3">
          <el-button v-if="type=='add'" @click="create('ruleForm')" class="myBgGreen">新 增</el-button>
          <el-button v-if="type=='update' && isShow(121)" @click="modify('ruleForm')" class="myBgGreen">保 存</el-button>
          <el-button @click="onBack" class="myBgGreen">返 回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
  .myVA_M{
    vertical-align: middle;
  }
</style>
<script>
  import { getFloorDetail, modifyFloor, createFloor } from '@/api/floor';
  import { getCellDetail, getCellList } from '@/api/cell';
  import { getBuildingList } from '@/api/building';
  import { getAreaList, getDistrictList } from '@/api/area';
  export default {
    data(){
      return{
        title:"楼层管理",
        id: null,
        detail: {},

        type:'',
        is_modifly:true,
        is_disabled:false,

        loading: false,
        

        districtList: [],
        districtLoaded: false,
        districtSelected: null,

        areaList: [],
        areaLoaded: false,
        areaSelected: null,

        buildingList: [],
        buildingLoaded: false,
        buildingSelected: null,

        cellList: [],
        cellLoaded: false,
        cellSelected: null,

        pqList:[
          {id:1,name:"片区1"},
          {id:1,name:"片区1"}
        ],
        district_id:-1,
        selected:-1,

        xqList:[
          {id:1,name:"片区1"},
          {id:1,name:"片区1"}
        ],
        area_id:-1,
        selectedXq:-1,

        ldList:[
          
        ],
        building_id:-1,
        selectedLd:-1,

        dyList:[
          
        ],
        cell_id:-1,
        selectedDy:-1,

        floor_name:"",
        floor_no:"",
        extra_desc:"",

        building_id:-1,
        selectedLd:"-1",
        xqdlist:[
          
        ],

        ruleForm:{},
        rules: {
          districtSelected: [
            {required: true, message: '请选择所属片区', trigger: 'change' }
          ],
          areaSelected:[
            {required: true, message: '请选择所属小区', trigger: 'change'}
          ],
          buildingSelected:[
            {required: true, message: '请选择楼栋名称', trigger: 'change'}
          ],
          cellSelected:[
            {required: true, message: '请选择单元名称', trigger: 'change'}
          ],
          floor_name:[
            {required: true, message: '请输入楼层名称', trigger: 'blur'}
          ]
        }
      }
    },
    components: {
        
    },
    created(){
      this.type = this.$route.query.type;
      this.id = this.$route.query.id;

      if(this.type=='update'){ //修改
        this.fetchDetail();
        this.is_modifly = false;
      }

      if(this.type=='add'){
        this.is_modifly = false;
      }

      if(this.type=='info'){ //修改
        this.fetchDetail();
        this.is_modifly = true;
        this.is_disabled = true;
      }
      
    },
    methods:{     
      fetchDetail() {
        let vm = this;
        vm.loading = true;

        getFloorDetail(vm.id)
        .then(response => {
          vm.ruleForm = response.data;

          vm.ruleForm.districtSelected = vm.ruleForm.district_info.value;
          vm.$set(vm.districtList, 0, vm.ruleForm.district_info);

          vm.ruleForm.areaSelected = vm.ruleForm.area_info.value;
          vm.$set(vm.areaList, 0, vm.ruleForm.area_info);

          vm.ruleForm.buildingSelected = vm.ruleForm.building_info.value;
          vm.$set(vm.buildingList, 0, vm.ruleForm.building_info);

          vm.ruleForm.cellSelected = vm.ruleForm.cell_info.value;
          vm.$set(vm.cellList, 0, vm.ruleForm.cell_info);

          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        })
      },

      clickDistrictSelect() {
        let vm = this;
        if (!vm.districtLoaded) {
          getDistrictList()
          .then(response => {
            vm.districtList = response.data;
            vm.districtLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      changeDistrictSelect() {
        this.areaLoaded = false;
        this.areaSelected = null;
        this.areaList = [];

        this.changeAreaSelect();
      },

      clickAreaSelect(val) {
        let vm = this;
        const params = {
          ril: 'S',
          district_id: vm.ruleForm.districtSelected,
          page_num: 1,
          page_size: 999
        };
        if (!vm.areaLoaded) {
          getAreaList(params)
          .then(response => {
            vm.areaList = response.data.rows;
            vm.areaLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      changeAreaSelect() {
        this.buildingLoaded = false;
        this.buildingSelected = null;
        this.buildingList = [];

        this.changeBuildingSelect();
      },

      clickBuildingSelect(val) {
        let vm = this;
        const params = {
          ril: 'S',
          area_id: vm.ruleForm.areaSelected,
          page_num: 1,
          page_size: 999
        };
        if (!vm.buildingLoaded) {
          getBuildingList(vm.ruleForm.areaSelected, params)
          .then(response => {
            vm.buildingList = response.data.rows;
            vm.buildingLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      changeBuildingSelect() {
        this.cellLoaded = false;
        this.cellSelected = null;
        this.cellList = [];
      },
      clickCellSelect() {
        console.log('clickCellSelect');
        if (this.ruleForm.buildingSelected == null) {
          return;
        }
        let vm = this;
        const params = {
          ril: 'S',
          building_id: vm.ruleForm.buildingSelected,
          page_num: 1,
          page_size: 999
        };
        if (!vm.cellLoaded) {
          getCellList(vm.ruleForm.areaSelected, params)
          .then(response => {
            vm.cellList = response.data.rows;
            vm.cellLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      modify(formName){
        let vm = this;
        vm.ruleForm.cell_id = vm.ruleForm.cellSelected;
        vm.ruleForm.area_id = vm.ruleForm.areaSelected;
        vm.$refs[formName].validate((valid) => {//验证
          if (valid) {
            vm.loading = true;
            modifyFloor(vm.id, vm.ruleForm)
            .then(response => {
              vm.$message.success("执行成功！");
              vm.loading = false;
              vm.onBack();
            })
            .catch(error=> {
              vm.loading = false;
            });
          } else {
            vm.$message.error("执行失败！");
            return false;
          }
        });
      },
      create(formName){
        let vm = this;
        vm.ruleForm.cell_id = vm.ruleForm.cellSelected;
        vm.ruleForm.area_id = vm.ruleForm.areaSelected;
        vm.$refs[formName].validate((valid) => {//验证
          if (valid) {
            vm.loading = true;
            createFloor(vm.ruleForm)
            .then(response => {
              vm.$message.success("执行成功！");
              vm.loading = false;
              vm.onBack();
            })
            .catch(error=> {
              vm.loading = false;
            });
          } else {
            vm.$message.error("执行失败！");
            return false;
          }
        }); 
      },
      onBack(){
        let vm=this;
        window.localStorage.setItem('pageNum',vm.$route.query.pageNum||1);
        vm.$router.go(-1);
      }


    }
  
  }


</script>



